package server.tool;


import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.system.SystemUtil;
import org.junit.jupiter.api.Order;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration(value = "sys_module", enforceUniqueMethods = false)
@Scope(value = "singleton")
public class SystemInfoTool {
    private static final JSONObject info = new JSONObject();


    @Bean
    @Order(1)
    public void setInfo() {
        Console.log("load config",SystemUtil.getJavaInfo());
        if (info.isEmpty()) {
            info.putOpt("java", SystemUtil.getJavaInfo())
                    .putOpt("jvm", SystemUtil.getJvmInfo())
                    .putOpt("spec", SystemUtil.getJavaSpecInfo())
                    .putOpt("java_run_time", SystemUtil.getJavaRuntimeInfo())
                    .putOpt("os", SystemUtil.getOsInfo())
                    .putOpt("user", SystemUtil.getUserInfo())
                    .putOpt("host", SystemUtil.getHostInfo())
                    .putOpt("run_time", SystemUtil.getRuntimeInfo());
//            Console.log(SystemUtil.getJavaInfo(),SystemUtil.getJvmInfo());
        }
    }

    @Bean
    @Scope(value = "singleton")
    @Order(2)
    public JSONObject getMachineInfo() {
        return info;
    }

    @Bean
    @Scope(value = "singleton")
    @Order(3)
    public JSONObject getMachineInfoArgs(SystemInfo... args) {
        JSONObject res;
        if (args.length == 0) {
            res = info;
        } else {
            res = new JSONObject();
            int i = 0;
            int len = args.length;
            while (i < len) {
                String k = args[i].getStr();
                if (StrUtil.isNotEmpty(k)) {
                    res.putOpt(k, info.get(k));
                }
                i++;
            }
        }
        return res;
    }
}

